package generator;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.apache.http.HttpResponse;

import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

public class ResponseGenerator {
    public static DefaultFullHttpResponse getResponse(HttpResponseStatus status, ByteBuf content, HttpResponse endpointResponse)
    {
        DefaultFullHttpResponse response= new DefaultFullHttpResponse(HTTP_1_1, status, content);
        response.headers().set("Content-Type", "application/json");
        //200和异常的响应的请求头有别
        if(status==HttpResponseStatus.OK)
        {
            response.headers().setInt("Content-Length", Integer.parseInt(endpointResponse.getFirstHeader("Content-Length").getValue()));
            return response;
        }
        else
        {
            response.headers().setInt("Content-Length", response.content().readableBytes());
            return response;
        }
    }
}
